﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using HY_DAL;
using BEM.Util;
namespace HY_BLL
{
    public class HY_UserBLL
    {
       
        private HY_UserDAO hyuserdao = null;

        public HY_UserBLL()
        {
            hyuserdao = new HY_UserDAO();
        }

        /// <summary>
        /// 进行登录判断
        /// </summary>
        /// <param name="strUserName">用户名</param>
        /// <returns>返回登录状态</returns>
        public int UserLogJude(HY_Model.User mUser)
        {
            string strUserName = mUser.Yhdlm;
            string strUserPwd=mUser.Yhkl;
            int nStates = 100;
            try
            {
                DataTable dt1 = new DataTable();
                dt1=hyuserdao.GetUserInfo(strUserName);
                if (dt1.Rows.Count != 0)
                {
                    //维基加密

                    //用户输入的密码
                    string strUserNewPwd = "";
                    DESEncryptor desEncryptor2 = new DESEncryptor();
                    desEncryptor2.EncryptKey = "PLAT_CDS";              //8为长度
                    desEncryptor2.InputString = strUserPwd;
                    desEncryptor2.DesEncrypt();
                    strUserNewPwd = desEncryptor2.OutString;

                    //根据用户名查询出的密码
                    string strUserOldPwd = "";
                    DESEncryptor desEncryptor1 = new DESEncryptor();
                    desEncryptor1.EncryptKey = "PLAT_CDS";              //8为长度
                    desEncryptor1.InputString = dt1.Rows[0]["yhkl"].ToString().Trim();
                    desEncryptor1.DesEncrypt();
                    strUserOldPwd = desEncryptor1.OutString;
                    

                    //用户名密码错误
                    if (strUserNewPwd.Equals(strUserOldPwd) != true)
                    {
                        nStates = 102;

                    }
                    else
                    {
                        //舰艇部队后勤保障
                        DataTable dt2 = hyuserdao.GetAaa_5_datesUserName(strUserName);

                        //航空兵场站保障
                        DataTable dt3 = hyuserdao.GetAcz_5_datesUserName(strUserName);
                        //无此登录人保障部队信息
                        if (dt2.Rows.Count == 0 && dt3.Rows.Count == 0)
                        {
                            nStates = 103;
                        }else
                        {
                            if (dt2.Rows.Count > 0)
                            {
                                //舰艇部队后勤保障
                                nStates = 105;
                            }
                            else 
                            {
                                //舰艇部队后勤保障
                                nStates = 106;
                            }
                        }
                    }

                }
                else
                {
                    //用户名不存在
                    nStates = 101;
                }
               
            }
            catch (Exception EX)
            {
                //登录异常请联系管理员
                nStates = 100;
            }

            return nStates;
        }
        
    }
}
